package com.service.bean.garbage;

import java.sql.Timestamp;
import java.text.MessageFormat;

import com.service.bean.device.Sensor;
import com.service.bean.device.Vehicle;
import com.service.bean.signal.GpsSignal;
import com.service.other.Logger;
import com.service.tools.Tools;

/**
 * 车辆称重用接口所需参数拼接
 * @author lj
 *
 */
public class SendTrackData extends GarbageSend {

	public SendTrackData() {}
	
	public SendTrackData(Vehicle vehicle, GpsSignal signal) {
		this.vehicle = vehicle;
		this.signal = signal;
	}

	/**
	 * 车辆是否在装载垃圾
	 * @return <pre>0:不在装载</br>1:在装载</pre>
	 */
	private int checkIsTagUp() {
		try {
			if (!"03".equals(vehicle.kindCode) && !"07".equals(vehicle.kindCode))
				// 既不是压缩车也不是餐厨车, 没有装载状态
				return 0;
			if (0 == signal.accStatus)
				// 车辆未点火, 不在装载中
				return 0;
			if (signal.speed > 5)
				// 车速大于5, 不在装载中
				return 0;
			if (signal.strengthStatus == 0 
					&& ((signal.equipmentType == 1 && signal.binStatus == 0) 
							|| (signal.equipmentType == 3 && signal.stuffStatus == 0)))
				// 车辆取力器和填装箱/垃圾箱都未打开, 不在装载中
				return 0;
			if (null == vehicle.garbagePoints || 0 == vehicle.garbagePoints.size())
				// 车辆未绑定垃圾收集点, 不在装载中
				return 0;
			// 非以上条件, 就是在装载中
			return 1;
		} catch (Exception e) {
			Logger.err(e);
		}
		return 0;
	}
	
	/**
	 * 车辆是否在卸载垃圾
	 * @return <pre>0:不在卸载</br>1:在卸载</pre>
	 */
	private int checkIsTagDown() {
		try {
			if (!"03".equals(vehicle.kindCode) && !"07".equals(vehicle.kindCode))
				// 既不是压缩车也不是餐厨车, 没有卸载状态
				return 0;
			if (0 == signal.accStatus)
				// 车辆未点火, 不在卸载中
				return 0;
			if (signal.speed > 5)
				// 车速大于5, 不在卸载中
				return 0;
			if ((signal.equipmentType == 1 && signal.binStatus == 0)
					|| (signal.equipmentType == 3 && signal.stuffStatus == 0))
				// 填装箱/垃圾箱未打开，不在卸载中
				return 0;
			if (null == vehicle.dumpPoints || 0 == vehicle.dumpPoints.size())
				// 该车辆没有绑定垃圾收集点
				return 0;
			// 非以上条件, 就是在卸载中
			return 1;
		} catch (Exception e) {
			Logger.err(e);
		}
		return 0;
	}
	
	/**
	 * 判断车辆是否安装了油位传感器
	 * @return <pre>1: 未安装油位传感器</br>0: 已安装油位传感器</pre>
	 */
	private int isOilSensor() {
		if (null == vehicle || vehicle.sensorMap.isEmpty())
			return 1;
		for (Sensor s : vehicle.sensorMap.values()) {
			if (s.isOilSensor())
				return 0;
		}
		return 1;
	}
	
	/**
	 * 用于给拼接Json字符串
	 * @return
	 */
	private Object[] getParamsForJson() {
		Object[] arrs = new Object[16];
		arrs[0] = "\"" + mskey + "\""; // msKey 秘钥
		arrs[1] = "\"" + vehicle.equipmentId + "\""; // CarNo 车辆编号
		arrs[2] = "\"" + vehicle.plateNO + "\""; // PlateNo 车牌号
		arrs[3] = "\"" + signal.gpsNo + "\""; // DeviceNo 设备编号
		arrs[4] = "\"" + String.valueOf(signal.lng) + "\""; // Lng 经度
		arrs[5] = "\"" + String.valueOf(signal.lat) + "\""; // Lat 纬度
		arrs[6] = "\"" + String.valueOf(Tools.round(signal.direction, 0)) + "\""; // Direction 方向
		arrs[7] = "\"" + String.valueOf(signal.speed) + "\""; // Speed 当前速度
		arrs[8] = "\"" + String.valueOf(signal.oilAmtCal) + "\""; // Oil 实际油量
		arrs[9] = "\"" + String.valueOf(signal.totalMile) + "\""; // Mileage 累计里程
		arrs[10] = "\"" + String.valueOf(checkIsTagUp()) + "\""; // TagUp 装载标志 1装载 0常态
		arrs[11] = "\"" + String.valueOf(checkIsTagDown()) + "\""; // TagDown 卸载标志 1卸载 0常态
		arrs[12] = signal.connectPower == 0 ? ("\"" + String.valueOf(1) + "\"") : ("\"" + String.valueOf(0) + "\""); // 主电源断电标志 1断电 0通电
		arrs[13] = "\"" + String.valueOf(signal.terminalFault) + "\""; // 0 GPS正常  1 GPS开路
		arrs[14] = "\"" + isOilSensor() + "\""; // 是否安装了油杆，1是未安装，0是已安装
		arrs[15] = signal.gpsTime == null ? ("\"" + signal.gpsTime + "\"") : ("\"" + new Timestamp(signal.gpsTime.getTime()) + "\""); // GPS时间
		return arrs;
	}
	
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("\"MSKey\":{0},");
		buffer.append("\"CarNo\":{1},");
		buffer.append("\"PlateNo\":{2},");
		buffer.append("\"DeviceNo\":{3},");
		buffer.append("\"Lng\":{4},");
		buffer.append("\"Lat\":{5},");
		buffer.append("\"Direction\":{6},");
		buffer.append("\"Speed\":{7},");
		buffer.append("\"Oil\":{8},");
		buffer.append("\"Mileage\":{9},");
		buffer.append("\"TagUp\":{10},");
		buffer.append("\"TagDown\":{11},");
		buffer.append("\"TagOff\":{12},");
		buffer.append("\"TagNoGPS\":{13},");
		buffer.append("\"TagNoOil\":{14},");
		buffer.append("\"Time\":{15}");
		return MessageFormat.format(buffer.toString(), getParamsForJson());
	}
	
}
